package logic

import (
	"context"
	"week1/ordersrv/service"

	"week1/ordersrv/internal/svc"
	"week1/ordersrv/ordersrv"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetOrderGoodsCountLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetOrderGoodsCountLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetOrderGoodsCountLogic {
	return &GetOrderGoodsCountLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetOrderGoodsCountLogic) GetOrderGoodsCount(in *ordersrv.GetOrderGoodsCountReq) (*ordersrv.GetOrderGoodsCountRes, error) {
	// todo: add your logic here and delete this line
	var (
		list []*ordersrv.GoodsDetailInfo
	)
	res, err := service.GetGoodsDetailByOrderSn(in.OrderSn)
	//判断库存是否充足
	if err != nil {
		return nil, err
	}
	for _, v := range res {
		list = append(list, &ordersrv.GoodsDetailInfo{
			GoodId:  uint32(v.GoodId),
			GoodNum: uint32(v.GoodsStock),
		})
	}
	return &ordersrv.GetOrderGoodsCountRes{GoodsDetailInfo: list}, nil
}
